package com.xigua.JucOfBeautiful.chapterOne.notifyAndWait1_3;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: 不同于在共享变量上调用notify函数会唤醒被阻塞到该共享变量上的一个线程，notifyAll函数
 * 则会唤醒所有在该共享变量上由于调用wait系列方法而被挂起的线程。
 * @date 2022/1/2221:28
 */
public class NotifyAllTest {
    private static volatile  Object resourceA = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread threadA = new Thread(()->{
            synchronized (resourceA){
                System.out.println("threadA get resourceA lock");
                try {
                    System.out.println("threadA begin wait");
                    resourceA.wait();
                    System.out.println("threadA end wait");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread threadB = new Thread(()->{
            synchronized (resourceA) {
                System.out.println("threadB get resourceA lock");
                try {
                    System.out.println("threadB begin wait");
                    resourceA.wait();
                    System.out.println("threadB end wait");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread threadC = new Thread(()->{
            synchronized (resourceA) {
                System.out.println("threadC begin begin notify");
                resourceA.notifyAll();
            }
        });
        threadA.start();
        threadB.start();
//        Thread.sleep(1000);
        threadC.start();
        threadA.join();
        threadB.join();
        threadC.join();
        System.out.println("main over");

    }
}
